const path = require("path");
const fs = require("fs");
exports.upload = (req, res, next) => {
  const file = req.file;
  // console.log(file);
  originalname = file.originalname;
  // console.log(originalname);
  nameArray = originalname.split("");
  var nameMime = [];
  l = nameArray.pop();
  nameMime.unshift(l);
  while (nameArray.length != 0 && l != ".") {
    l = nameArray.pop();
    nameMime.unshift(l);
  }
  //Mime是文件的后缀
  Mime = nameMime.join("");
  //重命名文件 加上文件后缀
  fs.renameSync("./upload/" + file.filename, "./upload/" + file.filename + Mime);
  res.send({
    url: `http://39.108.213.71:8080/papi/file/download/${file.filename + Mime}`,
    name: originalname,
  });
};
exports.download = async (req, res) => {
  let file_name = req.params.name;
  let file_path = process.cwd() + "/upload/" + file_name;
  res.download(file_path);
};
